package com.william.jdbcplus.core.util.druid;

import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitor;

import java.text.SimpleDateFormat;
import java.util.Date;

public class MySqlOutputVisitor extends com.alibaba.druid.sql.dialect.mysql.visitor.MySqlOutputVisitor implements MySqlASTVisitor {

    public MySqlOutputVisitor(Appendable appender) {
        super(appender);
    }

    public MySqlOutputVisitor(Appendable appender, boolean parameterized) {
        super(appender, parameterized);
    }

    @Override
    public void print(Date date) {
        if (this.appender == null) {
            return;
        }

        String DateSql = formatDate(date);
        print0(DateSql);
    }

    private String formatDate(Date date) {
        if (date instanceof java.sql.Time) {
            return "'" + new SimpleDateFormat("HH:mm:ss").format(date) + "'";
        } else if (date instanceof java.sql.Date) {
            return "'" + new SimpleDateFormat("yyyy-MM-dd").format(date) + "'";
        } else {
            return "'" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date) + "'";
        }
    }
}
